package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/9/30 15:55
 * @description
 *
 */
@Slf4j
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result list(@RequestParam(required = false) String name,
                       @RequestParam(required = false) String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询角色,参数:角色名称={},角色标签={},当前页码={},每页大小={}", name, label, page, pageSize);
        PageResult<Role> pageResult = roleService.page(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色：role={}", role.toString());
        roleService.addRole(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id) {
        log.info("查询角色：id={}", id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }
    @LogAnno
    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        log.info("修改角色：role={}", role.toString());
        roleService.updateRole(role);
        return Result.success();
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        log.info("删除角色：id={}", id);
        roleService.deleteRole(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAll() {
        log.info("查询所有角色");
        List<Role> list = roleService.getAll();
        return Result.success(list);
    }
}
